// pages/index/search/index.js
const App = getApp();
const common = require("../../../common/common")
const AJAX = require("../../../common/request")
const API = require("../../../common/api")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        options: null,
        display: false,
        navTop: 0,
        navContentHeight: 0,
        placeholderHeight: 0,
        seachText: "",
        alllist:[],
        mylist:[],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            navTop: App.globalData.navTop,
            navContentHeight: App.globalData.navContentHeight,
            options: JSON.stringify(options)
        })
        const query = wx.createSelectorQuery();
        query.select('.head').boundingClientRect();
        query.exec((res) => {
            this.setData({
                placeholderHeight: res[0].height
            })
        });
        this.getbSearchoflist()
    },
    getbSearchoflist(){
        console.log('asdfasfd')
        common.checkLogin().then(()=>{
            AJAX.POST("/system/AppWechat/getbSearchoflist",{
                wechatUsersId:App.globalData.user.wechatUserId,
            }).then((res)=>{
                console.log(res.data.alllist)
                this.setData({
                    alllist:res.data.alllist,
                    mylist:res.data.mylist,
                })
            })
        })
    },
    onShow(){
        this.getbSearchoflist()
    },
    drugIntroductionNameInput: function (e) {
        this.setData({
           seachText: e.detail.value
        })
    },
    goBack() {
      const pages = getCurrentPages();
      //获取不到上级返回首页
      if (pages.length > 1) {
          wx.navigateBack({})
      } else {
          wx.reLaunch({
              url: '/pages/index/index',
          })
      }
  },
    goSearch(){
        if(this.data.seachText == ""){
            return
        }
        AJAX.POST('/system/AppWechat/addbSearchof',{
            wechatUsersId:App.globalData.user.wechatUserId,
            searchofHotwords:this.data.seachText
        })
        this.goSearchList(this.data.seachText)
    },
    goItemSearch(e){
        this.goSearchList(e.currentTarget.dataset.text)
    },

    goSearchList(e){
        this.getbSearchoflist()
        wx.navigateTo({
          url: '/pages/index/drug/list/index?seach_text='+e,
        })
    },
    onShareAppMessage(options) {
		// 默认分享配置
		return {
			title: App.globalData.onShareAppMessage.title,
			path: App.globalData.onShareAppMessage.path,
			imageUrl: App.globalData.onShareAppMessage.imageUrl,
		}
    },
    //清空个人搜索
    delMyState(){
        AJAX.POST('/system/AppWechat/updateMyState',{
            wechatUsersId:App.globalData.user.wechatUserId,
            state:1,
        }).then(res=>{
            if(res.code == 200){
                this.setData({
                    mylist:[]
                })
            }
        })
    }
})